package com.ubiquando.api.server;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ubiquando.api.server.db.Client;
import com.ubiquando.api.server.db.Dao;

public class Connect extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7610770810009719555L;

	private static final Logger log = Logger.getLogger(Register.class.getName());

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		resp.setContentType("application/json");
		String email = req.getParameter("email");
		String pwd = req.getParameter("pwd");
		boolean connection = Boolean.parseBoolean(req.getParameter("connection"));

		//		System.out.println("client id is "+clientId+"\nand connection is "+connection);

		log.info("In register servlet received: email="+email+" pwd="+pwd+" connection="+connection);

		Client client = Dao.INSTANCE.connect(email, pwd, connection);
		if(client != null){
			MyJsonWriter.returnWallet(resp.getWriter(), client);
			log.info("Client "+email+" was connected succesfuly");
		}else{
			MyJsonWriter.returnError(resp.getWriter(), "Client not found");
			log.info("Client "+email+" did not connect successfuly");
		}
	}

}
